package cheme.ProcessModel.Table;

import cheme.ProcessModel.AbstractParent;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.Errors.ChemeNullArgError;

public abstract class Row extends AbstractParent<Field> implements IRow
{
	/*
	 * A tag having an isInitial state of true means that it is a satisfatory dummy tag for 
	 * presenting the user a 'new tag component' edit target as a means of adding new tag components
	 * to a tag list. If a tag list ever has a last component that is non-initial, then a new
	 * one may be added to maintain the 'new tag component' edit target.
	 */
	protected boolean isInitial = true;
	
	public Row(IModelElement parent)
	{
		super(parent);
	}

	protected void setInternal(int index, String text)
	{
		if(getChildren().size() < index)
		{
			throw new ChemeNullArgError();
		}
		
		if(get(index) instanceof IWritableField)
		{
			((IWritableField)get(index)).setText(text);
		}
	}
	
	/**
	 * @param label the label to set
	 */
	public void set(int index, String text)
	{
		if(!get(index).equals(text))
		{
			setInternal(index, text);
		}
	}
	
	/**
	 * @param label the label to set
	 */
	public void set(int index, String text, String updateProperty)
	{
		if(!get(index).equals(text))
		{
			String old = get(index).getText();			
			setInternal(index, text);
		
			firePropertyChange(updateProperty, old, text);			
		}
	}
	
	public String getAt(int index)
	{
		return get(index).getText();
	}
		
	@Override
	protected String getToStringSeparator()
	{
		return " | ";
	}
}